# coding=utf-8
"""
    数据模型目录

    暂未引入, 需配合 Flask-SQLAlchemy 使用, 当前并不需要 ORM 映射,

    定义类和数据库表建立映射关系
"""

# 示例 :
'''
# 关联表 - 用户角色表 (中间表)
user_roles = db.Table('user_roles',
    db.Column('user_id', db.Integer, db.ForeignKey('users.id')),
    db.Column('role_id', db.Integer, db.ForeignKey('roles.id'))
)

# User Model :
class User(db.Model):
    __tablename__ = 'users'

    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(50), unique=True, nullable=False)
    email = db.Column(db.String(100), unique=True, nullable=False)
    password_hash = db.Column(db.String(128), nullable=False)

    # 关系：用户拥有多个角色
    roles = db.relationship('Role', secondary=user_roles, backref=db.backref('users', lazy='dynamic'))

    # 关系：用户拥有多个订单
    orders = db.relationship('Order', backref='user', lazy=True)

    def __init__(self, username, email, password):
        self.username = username
        self.email = email
        self.set_password(password)

    def set_password(self, password):
        """
        生成哈希密码
        """
        self.password_hash = generate_password_hash(password)

    def check_password(self, password):
        """
        验证哈希密码
        """
        return check_password_hash(self.password_hash, password)

    def __repr__(self):
        return f'<User {self.username}>'


# Role Model :
class Role(db.Model):
    __tablename__ = 'roles'

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(50), unique=True)

    def __repr__(self):
        return f'<Role {self.name}>'


# Order Model :
class Order(db.Model):
    __tablename__ = 'orders'

    id = db.Column(db.Integer, primary_key=True)
    order_number = db.Column(db.String(50), unique=True)
    total_price = db.Column(db.Float, nullable=False)

    # 外键：关联用户表
    user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False)

    def __repr__(self):
        return f'<Order {self.order_number}>'
'''

# @Time    :  2024-10-16 18:21:32
# @Author  :  jiangtong
# @Email   :  gingerqgyy@outlook.com
# @Project :  ginger_yl_test_boost
# @File    :  __init__.py
